package com.bm.specialvehiclefordispatcher.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bm.specialvehiclefordispatcher.R;

/**
 * 选择产品数量控件
 * 
 * @Copyright Copyright © 2014 蓝色互动. All rights reserved.
 * 
 * @author ZhuFan
 * @Date: 2015-4-9
 */
public class SelectCountView extends LinearLayout implements View.OnClickListener {

	/**
	 * 页面控件
	 */
	private ImageView plus, minus;
	private TextView countView;

	/**
	 * 选择的数量
	 */
	private int count = 0;

	/**
	 * 最大数量
	 */
	private int maxCount = Integer.MAX_VALUE;

	/**
	 * 监听器
	 */
	private OnCountChangedListener listener;

	/**
	 * 是否可用
	 */
	private boolean isEnabled = true;

	public SelectCountView(Context context) {
		super(context);
		init(context);
	}

	public SelectCountView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	/**
	 * 获取当前控件的可用状态
	 */
	@Override
	public boolean isEnabled() {
		return isEnabled;
	}

	/**
	 * 设置当前控件的可用状态
	 */
	@Override
	public void setEnabled(boolean isEnabled) {
		this.isEnabled = isEnabled;
		if (isEnabled)
			countView.setTextColor(0xFF666666);
		else
			countView.setTextColor(0xFFCCCCCC);
	}

	/**
	 * 注册监听器
	 * 
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @param listener
	 * @return void
	 * @throws
	 * @Date 2015-4-9 上午9:49:48
	 */
	public void setOnCountChangedListener(OnCountChangedListener listener) {
		this.listener = listener;
	}

	/**
	 * 获取当前控件选择的数量
	 * 
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @return
	 * @return int
	 * @throws
	 * @Date 2015-4-9 上午9:50:01
	 */
	public int getCount() {
		return count;
	}

	/**
	 * 设置数量
	 * 
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @param count
	 * @return void
	 * @throws
	 * @Date 2015-4-9 上午9:50:14
	 */
	public void setCount(int count) {
		this.count = count;
		setText();
	}

	/**
	 * 获取当前控件的最大数量
	 * 
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @return
	 * @return int
	 * @throws
	 * @Date 2015-4-9 上午9:50:23
	 */
	public int getMaxCount() {
		return maxCount;
	}

	/**
	 * 设置最大数量, 应根据库存设置
	 * 
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @param maxCount
	 * @return void
	 * @throws
	 * @Date 2015-4-9 上午9:50:32
	 */
	public void setMaxCount(int maxCount) {
		// this.maxCount = maxCount;
	}

	/**
	 * 初始化
	 * 
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @param context
	 * @return void
	 * @throws
	 * @Date 2015-4-9 上午9:50:52
	 */
	private void init(Context context) {
		LayoutInflater.from(context).inflate(R.layout.view_select_count, this);

		plus = (ImageView) findViewById(R.id.iv_plus);
		minus = (ImageView) findViewById(R.id.iv_min);
		countView = (TextView) findViewById(R.id.tv_count);

		plus.setOnClickListener(this);
		minus.setOnClickListener(this);
//		plus.setOnTouchListener(touch);
//		minus.setOnTouchListener(touch);
//		plus.setTag(true);
//		minus.setTag(false);

		setText();
	}

	OnTouchListener touch = new OnTouchListener() {
		static final long clickTime = 400;
		long down;
		long current;

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (isEnabled) {
				boolean flag = (Boolean) v.getTag();

				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					down = System.currentTimeMillis();
					break;
				case MotionEvent.ACTION_MOVE:
					current = System.currentTimeMillis();
					if (current - down > clickTime) {
						if (flag) {
							if (count < maxCount)
								count++;
						} else {
							if (count > 1)
								count--;
						}

						setText();
					}
					break;
				case MotionEvent.ACTION_UP:
					v.performClick();
					break;
				}
			}
			return true;
		}
	};

	/**
	 * 设置显示字符
	 * 
	 * @author ZhuFan
	 * @Package com.bm.zhengpinmao.views
	 * @return void
	 * @throws
	 * @Date 2015-4-9 上午9:50:58
	 */
	private void setText() {
		countView.setText(count + "");
	}

	@Override
	public void onClick(View v) {
		if (isEnabled) {
			switch (v.getId()) {
			case R.id.iv_plus:
				if (count >= maxCount)
					return;
				count++;
				setText();
				break;

			case R.id.iv_min:
				if (count <= 0)
					return;
				count--;
				setText();
				break;

			default:
				break;
			}
			if (listener != null)
				listener.onCountChanged(this, count);
		}
	}

	/**
	 * 注册一个回调, 当用户点击加或减时会回调此监听
	 * 
	 * @Copyright Copyright © 2014 蓝色互动. All rights reserved.
	 * 
	 * @author ZhuFan
	 * @Date: 2015-4-9
	 */
	public interface OnCountChangedListener {
		/**
		 * 当用户点击加或减时会回调此监听
		 * 
		 * @author ZhuFan
		 * @Package com.bm.zhengpinmao.views
		 * @param view
		 *            当前控件
		 * @param count
		 *            选择的数量
		 * @return void
		 * @throws
		 * @Date 2015-4-9 上午9:53:44
		 */
		public void onCountChanged(SelectCountView view, int count);
	}

}
